package com.hspedu.file;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author DL5O
 * @version 1.0
 * 演示创建文件
 */
public class FileCrate {
    public static void main(String[] args) {

    }

    //方法1 new File(String pathname);
    public static void create01(){
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);

        try {
            file.createNewFile();
            System.out.println("文件创建成功...");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件创建失败...");
        }
    }

    //方式二：new File(File parent,String child) //根据父目录文件+子路径构建
    //e:\\news2.txt
    //parent:e:\\
    //child:news2.txt
    public static void create02(){
        String parent = "e:\\";
        String child = "news2.txt";
        //这里的file对象，在java程序中，只是个对象而已
        //只有执行了createNewFile方法才进行创建
        File file = new File(parent, child);

        try {
            file.createNewFile();
            System.out.println("文件创建成功..");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    //方式3  new File(String parent,String child) //根据父目录+子路径构建
    public void create03(){
        String parentPath = "e:\\";
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);

        try {
            file.createNewFile();
            System.out.println("文件创建成功..");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
